<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Grom Hellscream
 * Date: 10/26/12
 * Time: 11:22 PM
 * To change this template use File | Settings | File Templates.
 */
defined('_JEXEC') or die;

class VastViewKhoi extends JViewLegacy
{
    protected $form;
    protected $item;
    protected $state;

    /**
     * Display the view
     */
    public function display($tpl = null)
    {
        // Initialiase variables.
        $this->form		= $this->get('Form');
        $this->item		= $this->get('Item');
        $this->state	= $this->get('State');
        $script = $this->get('Script');

        JLoader::register('KhoisHelper', JPATH_COMPONENT . '/helpers/khoi.php');

        $this->canDo	= KhoisHelper::getActions($this->state->get('filter.category_id'));

        $this->script = $script;

        // Check for errors.
        if (count($errors = $this->get('Errors'))) {
            JError::raiseError(500, implode("\n", $errors));
            return false;
        }

        $this->addToolbar();
        parent::display($tpl);

        // Set the document
        $this->setDocument();
    }

    /**
     * Add the page title and toolbar.
     *
     * @since	1.6
     */
    protected function addToolbar()
    {
        JRequest::setVar('hidemainmenu', true);
        $isNew		= ($this->item->id == 0);
        JToolBarHelper::title(JText::_('COM_VAST_KHOI_DETAIL'), 'head vm_manufacturer_48');

        if (!$isNew)
        {
            JFactory::getApplication()->setUserState('khoi_id', $this->item->id);
            JToolBarHelper::custom('donvitheokhoi.addCustom', 'new', 'new', 'Thêm đơn vị vào khối', false);
        }

        JToolBarHelper::custom('khoi.edit2donvi', 'checkin', 'checkin', 'COM_VAST_JTOOLBAR_DONVITHEOKHOI', false);

        JToolBarHelper::divider();

        JToolBarHelper::save2new('khoi.save2new', 'COM_VAST_KHOI_JTOOLBAR_SAVE2NEW');
        JToolBarHelper::save2copy('khoi.save2copy', 'COM_VAST_KHOI_JTOOLBAR_SAVE2COPY');
        JToolBarHelper::save('khoi.save', 'COM_VAST_KHOI_JTOOLBAR_SAVE');
        JToolBarHelper::cancel('khoi.cancel', $isNew ? 'COM_VAST_KHOI_JTOOLBAR_CANCEL' : 'COM_VAST_KHOI_JTOOLBAR_CLOSE');
    }
    /**
     * Method to set up the document properties
     *
     * @return void
     */
    protected function setDocument()
    {
        $isNew = ($this->item->id < 1);
        $document = JFactory::getDocument();
        $document->setTitle($isNew ? JText::_('COM_VAST_KHOI_CREATING')
            : JText::_('COM_VAST_KHOI_EDITING'));
        $document->addScript(JURI::root() . $this->script);
        $document->addScript(JURI::root() . "/administrator/components/com_vast"
            . "/views/khoi/submitbutton.js");
        JText::script('COM_VAST_KHOI_ERROR_UNACCEPTABLE');
    }
}